package cn.ctodb.im.server.handler;

import cn.ctodb.im.api.Command;
import cn.ctodb.im.api.Connection;
import cn.ctodb.im.dto.MessageRespDTO;
import cn.ctodb.im.dto.MessageTextReqDTO;
import cn.ctodb.im.handler.PacketHandler;
import cn.ctodb.im.proto.Message;
import cn.ctodb.im.rpc.MessageRpc;
import cn.ctodb.im.server.peer.PeerManager;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * All rights Reserved, Designed By www.ctodb.cn
 *
 * @version V1.0
 * @author: lichaohn@163.com
 * @date: 2018-11-07 16:01
 * @Copyright: 2018 www.ctodb.cn All rights reserved.
 */
@Component
public class MessageTextHandler extends PacketHandler<Message.TextReq> {

    @Reference(check = false)
    private MessageRpc messageRpc;
    @Autowired
    private PeerManager peerManager;

    @Override
    public Command cmd() {
        return Command.MESSAGE_REQ_TEXT;
    }

    @Override
    public void handle(Message.TextReq packet, Connection connection) {
        logger.debug("接收到请求：{} {}", cmd(), packet);
        MessageTextReqDTO messageTextReqDTO = new MessageTextReqDTO();
        // 追加发送人ID
        messageTextReqDTO.setFrom(peerManager.get(connection.getChc().channel().id().asLongText()).getUid());
        messageTextReqDTO.fromProto(packet);
        MessageRespDTO messageResp = messageRpc.sendText(messageTextReqDTO);
        connection.send(messageResp.toProto());
        logger.debug("msg content:{}", packet.getContent());
        logger.debug("反馈");
    }

}
